import { Component, OnInit } from '@angular/core';
import {HttpClient,HttpHeaders} from'@angular/common/http'


import { HttpserviceService } from '../../services/httpservice.service';
@Component({
  selector: 'app-news',
  templateUrl: './news.component.html',
  styleUrls: ['./news.component.scss']
})
export class NewsComponent implements OnInit {

  public List:any[]=[];

  constructor(public http:HttpClient,public service:HttpserviceService) { }

  ngOnInit(): void {
  }

  getData() {
  
    let api = 'http://a.itying.com/api/productList';
    //RxJS
    this.http.get(api).subscribe((response:any) =>{
        console.log(response);
        this.List=response.result;
    })
  }


  doLogin() {
    // alert('jjj')
      const httpOptions = {headers: new HttpHeaders({'Content-Type':'application/json'})};

      //存在跨域
      let api = 'http://127.0.0.1:3000/dologin';

      this.http.post(api,{"username":"张三","age":20},httpOptions).subscribe((reponse)=>{
          console.log(reponse);
      })
  }


  getJsonpData() {
    //jsonp请求 服务器必须得支持jsonp
    /*   http://a.itying.com/api/productList?callback=xxx
         http://a.itying.com/api/productList?cb=xxx
    */

         let api = 'http://a.itying.com/api/productList';
         this.http.jsonp(api,'callback').subscribe((reponse)=>{
           console.log(reponse);
         })
  }


  getAxiosData() {
    let api = 'http://a.itying.com/api/productList';
    this.service.axiosGet(api).then((data:any)=>{
        console.log(data)
    })
  }
}
